<template>
  <div style="background-color: black;">
    <div class="percent-chart" :ref="ids"></div>
  </div>
</template>

<script>
import * as echarts from "echarts";

export default {
  data() {
    return {
      percentChart: null,
      option: {}
    }
  },
  props: {
    percent:{
      type: Number,
      required: false,
      default: 30
    },
    ids:{
      type: String,
      required: false,
      default: 'percentChart'
    },
    color: {
      type: Array,
      required: false,
      default: ['#B8E5FF','#236DC9']
    }
  },
  watch:{
    percent:{
      handler(){
        if(this.percentChart){
          this.option.series[4].splitNumber = Math.ceil(  25 * (this.percent / 100))
          this.option.series[4].endAngle = 90 - ((this.percent / 100) * 360)
          this.option && this.percentChart.setOption(this.option,true);
        }else{
          this.initPercentChart()
        }
      }
    }
  },
  methods: {
    initPercentChart() {
      this.percentChart = null
      this.percentChart = echarts.init(this.$refs[this.ids]);
      // const percentChart = echarts.init(this.$refs[this.ids]);
      // percentChart.resize()
      // percentChart.clear()
      // const chartDom = document.getElementById(this.id);
      // this.percentChart = echarts.init(chartDom);
      // this.percentChart.resize()
      this.option = {
        angleAxis: {
          max: 100,
          show: false,
          startAngle: 0,
        },
        
        polar: {
          // radius: ['100%', '100%'],
          // center: ['35%', '50%'],
        },
        radiusAxis: {
          type: 'category',
          show: true,
          axisLabel: {
            show: false,
          },
          axisLine: {
            show: false,

          },
          axisTick: {
            show: false
          },
        },
        series: [
          
            //圈1
          {
          type: 'pie',
          radius: ['100%', '96%'],
          center: ['50%', '50%'],
          data: [
            {
              label: {
                show: false
              },
              labelLine: {
                normal: {
                  smooth: true,
                  lineStyle: {
                    width: 0
                  }
                }
              },
              value: 100,
              hoverAnimation: false,
              itemStyle: {
                color: new echarts.graphic.LinearGradient(
                    0, 0, 0, 1, [{//代表渐变色从正上方开始
                      offset: 0, //offset范围是0~1，用于表示位置，0是指0%处的颜色
                      color: '#273065'
                    }, //柱图渐变色
                      {
                        offset: 1, //指100%处的颜色
                        color: '#6578E5'
                      }
                    ]
                )
              },
            }
          ]
        },
          //圈2
          {
            type: 'pie',
            radius: ['90%', '86%'],
            center: ['50%', '50%'],
            data: [
              {
                label: {
                  show: false
                },
                labelLine: {
                  normal: {
                    smooth: true,
                    lineStyle: {
                      width: 0
                    }
                  }
                },
                value: 100,
                hoverAnimation: false,
                itemStyle: {
                  color: '#B2EDFA',
                },
              }
            ]
          },
          //圈3
          {
            type: 'pie',
            radius: ['60%', '56%'],
            center: ['50%', '50%'],
            data: [
              {
                label: {
                  show: false
                },
                labelLine: {
                  normal: {
                    smooth: true,
                    lineStyle: {
                      width: 0
                    }
                  }
                },
                value: 100,
                hoverAnimation: false,
                itemStyle: {
                  color: '#36437F',
                },
              }
            ]
          },
          //圈4
          {
            type: 'pie',
            radius: ['50%', '46%'],
            center: ['50%', '50%'],
            data: [
              {
                label: {
                  show: false
                },
                labelLine: {
                  normal: {
                    smooth: true,
                    lineStyle: {
                      width: 0
                    }
                  }
                },
                value: 100,
                hoverAnimation: false,
                itemStyle: {
                  color: '#1F2B60',
                },
              }
            ]
          },
          //齿轮
          {
            name: "大环",
            type: 'gauge',
            splitNumber: Math.ceil(  25 * (this.percent / 100)),
            radius:  '100%',
            center: ['50%', '50%'],
            startAngle: 90,//起始角度
            endAngle: 90 - ((this.percent / 100) * 360),//结束角度
            roundCap: true,
            axisLine: {
              show: false,
              lineStyle: {
                width:10,
                color: [
                  [1, new echarts.graphic.LinearGradient(
                      0, 0, 0, 1, [{//代表渐变色从正上方开始
                        offset: 0, //offset范围是0~1，用于表示位置，0是指0%处的颜色
                        color: this.color[0]
                      }, //柱图渐变色
                        {
                          offset: 1, //指100%处的颜色
                          color: this.color[1]
                        }
                      ]
                  )]
                ]
              }
            },
            axisTick: {
              show: false
            },
            splitLine: {
              show: true,
              length: 22,
              lineStyle: {
                color: 'auto',
                width: 16 * 0.3
              },
            },
            axisLabel: {
              show: false
            },
            detail: {
              show: false
            }
          },
        ]
      }
      this.option && this.percentChart.setOption(this.option);
      
    },
    resize(){
      if (this.percentChart) this.percentChart.resize();
    }
  },
  mounted() {
    window.addEventListener('resize', this.resize)
    this.$nextTick(() => {
      this.initPercentChart()
    })
  },
  beforeDestroy() {
    removeEventListener('resize',this.resize)
  }
}
</script>

<style scoped>
.percent-chart {
  width: 200px;
  height: 200px;
}
</style>